package com.atguigu.juc.interview.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author leimingchao
 * @create 2021-04-20 22:01
 *      java多线程中，第三种获得多线程的方式
 *          1.get方法一般请放在最后一行
 *
 */
class MyThread implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println("******come in here");
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 1024;
    }
}

public class CallableTest {
    public static void main(String[] args) {
        FutureTask futureTask=new FutureTask(new MyThread());
        Thread thread=new Thread(futureTask,"A");
        Thread thread2=new Thread(futureTask,"B");
        thread.start();
        thread2.start();
        try {
            System.out.println(Thread.currentThread().getName() + "****计算完成");
            System.out.println(futureTask.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}